package com.situ.controller;

import com.situ.bean.Product;
import com.situ.service.IProductService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api")
public class FilterController {

    @Autowired
    private IProductService productService;

    @GetMapping("/filter")
    public List<Product> filterProducts(
            @RequestParam(required = false) String categories,
            @RequestParam(required = false) Double minPrice,
            @RequestParam(required = false) Double maxPrice) {

        // 解析分类
        List<String> categoryList = new ArrayList<>();
        if (categories != null) {
            String[] categoryArray = categories.split(",");
            for (String category : categoryArray) {
                categoryList.add(category);
            }
        }
        System.out.println(categoryList.toString());
        List<Integer> categoryIds = new ArrayList<>();

        //循环访问categoryList,若是Dog Food  categoryIds.add(1)
        //
        //Cat Food
        //
        //Fish
        //
        //Pet Toy
        //
        //Parrot Food
        //
        //Rabbit
        //
        //Accessories
        //
        //Small Pet

        for (String category : categoryList) {
            switch (category) {
                case "dogfood":
                    categoryIds.add(1);
                    System.out.println("Dog Food");
                    break;
                case "catfood":
                    categoryIds.add(2);
                    System.out.println("Cat Food");
                    break;
                case "fish":
                    categoryIds.add(3);
                    System.out.println("Fish");
                    break;
                case "pettoy":
                    categoryIds.add(4);
                    System.out.println("Pet Toy");
                    break;
                case "parrotfood":
                    categoryIds.add(5);
                    System.out.println("Parrot Food");
                    break;
                    case "rabbit":
                        System.out.println("Rabbit");
                        categoryIds.add(6);
                        break;
                        case "accessories":
                            categoryIds.add(7);
                            System.out.println("Accessories");
                            break;
                            case "smallpet":
                                categoryIds.add(8);
                                System.out.println("Small Pet");
                                break;
                                default:
                                    break;

            }
        }

     System.out.println(categoryIds);


        List<Integer> pids = productService.findCategoryIdsByPids(categoryIds);


       System .out.println(productService.filterProducts(pids, minPrice, maxPrice).toString());

        // 调用服务层方法
        return productService.filterProducts(pids, minPrice, maxPrice);
    }
}